import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  UseInterceptors,
  UploadedFile
} from '@nestjs/common'
import * as xlsx from 'xlsx'
import { FileInterceptor } from '@nestjs/platform-express/multer'
import { ApiBody, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import { AccountService } from './account.service'
import { CreateAccountDto } from './dto/create-account.dto'
import { UpdateAccountDto } from './dto/update-account.dto'
import { FResponse } from 'src/utils/reponse'
@NoAuth()
@ApiTags('账套')
@Controller('account')
export class AccountController {
  constructor(private readonly awardRuleService: AccountService) {}

  @Post()
  @ApiOperation({ summary: '创建账套' })
  @ApiBody({ type: CreateAccountDto, description: '账套信息' })
  async create(@Body() createAccountDto: CreateAccountDto) {
    return FResponse.success(
      await this.awardRuleService.create(createAccountDto)
    )
  }
  @Post('upload')
  @ApiOperation({ summary: '批量创建账套' })
  @UseInterceptors(FileInterceptor('file'))
  async batchCreate(@UploadedFile() file) {
    const fileData = file.buffer
    const workbook = xlsx.read(fileData, { type: 'buffer' })
    const worksheet = workbook.Sheets[workbook.SheetNames[0]]
    const data: CreateAccountDto[] = xlsx.utils.sheet_to_json(worksheet)
    await Promise.all(data.map(item => this.awardRuleService.create(item)))
    return FResponse.success('批量创建成功')
  }
  @Post('all')
  @ApiOperation({ summary: '获取所有账套' })
  @ApiBody({
    description: '账套信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.awardRuleService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        isDelete: body.isDelete
      })
    )
  }

  // 获取真全部账套
  @Get('all')
  @ApiOperation({ summary: '获取真全部账套' })
  async findAllOption() {
    return FResponse.success(await this.awardRuleService.findAllOption())
  }

  @Patch(':id')
  @ApiOperation({ summary: '修改指定账套' })
  @ApiParam({ name: 'id', description: '账套ID' })
  @ApiBody({ type: UpdateAccountDto, description: '账套信息' })
  async update(
    @Param('id') id: string,
    @Body() updateAccountDto: UpdateAccountDto
  ) {
    return FResponse.success(
      await this.awardRuleService.update(+id, updateAccountDto)
    )
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除账套' })
  @ApiBody({ type: [Number], description: '账套ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.awardRuleService.remove(ids))
  }

  // 彻底删除
  @Delete('thorough')
  @ApiOperation({ summary: '批量彻底删除账套' })
  @ApiBody({ type: [Number], description: '账套ID数组' })
  async batchThoroughRemove(@Body() ids: number[]) {
    return FResponse.success(await this.awardRuleService.thoroughRemove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复账套' })
  @ApiBody({ type: [Number], description: '账套ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.awardRuleService.restore(ids))
  }
}
